var express = require('express');
const mysql = require("mysql");
var file = require('fs')
const zhaopin = express();
// var multer = require("multer")

//设置跨域访问
zhaopin.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next()
});

var connsql = mysql.createConnection({
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'root',
  database: 'zhaopin' // 数据库
})
connsql.connect() // 开启与数据库连接
console.log("数据库连接成功")

//接受传递过来的参数
const bodyParser = require('body-parser');
zhaopin.use(bodyParser.urlencoded({ extended: false }));
zhaopin.use(bodyParser.json());


/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

zhaopin.get('/job_all', function (req, res) {
  var selsql = "select * from job where pid = 0"
  var job = undefined
  connsql.query(selsql, (err, result) => {
    if (err) {
      console.log(err);
      return
    }
    for (let i = 0; i < result.length; i++) {
      let sql = "select * from job where pid = " + result[i].ID;
      connsql.query(sql, (err, results) => {
        if (err) {
          console.log(err);
          return;
        }
        result[i].children = results;
        if (i == result.length - 1) {
          job = {
            code: 1,
            data: {
              type: result
            }
          };
          res.send(job)
        }
      })
    }
  })
});




// zhaopin.get('/job', function (req, res) {
//   var sel = "select * from jobcs where pid = 0"
//   var job = undefined
//   var a = 0
//   connsql.query(sel, (err, result) => {
//     if (err) {
//       console.log(err);
//       return
//     }
//     for (let i = 0; i < result.length; i++) {
//       let sql1 = "select * from jobcs where pid = " + result[i].ID;
//       connsql.query(sql1, (err, results) => {
//         if (err) {
//           console.log(err);
//           return;
//         }
//         result[i].child = results;
//         for (let j = 0; j < result[i].child.length; j++) {
//           a++
//           let sql2 = "select * from jobcs where pid = " + result[i].child[j].ID;
//           connsql.query(sql2, (err, result1) => {
//             if (err) {
//               console.log(err);
//               return;
//             }
//             result[i].child[j].children = result1;
//             if(a == 23){
//               job = {
//                 code: 1,
//                 data: {
//                   type: result
//                 }
//               };
//               res.end(JSON.stringify(job))
//             }
//           })
//         }

//       })
//     }
//   })
// });








zhaopin.post("/job_dev", function (req, res) {
  var upsql = "update job set code = 0 where ID = '" + req.body.id + "'"
  connsql.query(upsql, (err, result) => {
    if (err) {
      console.log(err);
      return;
    }
    res.send({ code: 1, msg: "删除成功" })
  })
})



module.exports = zhaopin;
zhaopin.listen(3081, () => {
  console.log("启动3081");
})

